const app = getApp()
var upload = require('../../../utils/upload');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    group:{},
    fileList: [],
    tempFilePaths: [],
  },
  //打开载入
  onLoad: function (options) {
    if(app.globalData.appConfig.group.is_group == 0){
      app.wxAlert('你不是圈主',() =>{
        wx.navigateBack({
          delta:1
        })
      })
    }else{
      this.setData({
        group:app.globalData.appConfig.group.info,
        fileList:[{url:app.globalData.appConfig.group.info.face}]
      })
    }
  },
  
  /**
   * 提交绑定
   */
  formSubmit: function (e) {
    var data = e.detail.value;
    if (app.util().isNull(data.title)) {
      app.wxAlert('商圈名称必须填写');
    }else if (app.util().isNull(data.content)) {
      app.wxAlert('商圈简介必须填写');
    }else {
      this.setData({
        disabled:true
      })
      this.postFile((file) => {
        if (file.isend == true){
          var parms = {
            title: data.title,
            content: data.content,
            face: JSON.stringify(file.filePaths)
          }
          app.api().Post('api/v1/ais/gmgroup/edit', parms,(res)=>{
            this.setData({
              disabled:false
            })
            app.appConfig();
            app.wxAlert(res.msg,()=>{
              wx.navigateBack({
                delta:1
              })
            });
          })
          setTimeout(()=>{
            this.setData({
              disabled:false
            })
          },1500)
        }
      })
    }
  },
  //图片上传
  postFile: function (callback) {
    upload.Upfiles('api/v1/ais/config/upimg', this.data.tempFilePaths,callback);
  },
  //上传选择图片
  afterRead:function(event){
    const { file } = event.detail;
    var fileList = this.data.fileList;
    var tempFilePaths = this.data.tempFilePaths;
    fileList.push({url: file.path});
    tempFilePaths.push(file.path);
    this.setData({
      fileList:fileList,
    });
    this.data.tempFilePaths = tempFilePaths;
  },
  //删除图片
  deleteItem: function (res) {
    var tempFilePaths = this.data.tempFilePaths;
    var fileList = this.data.fileList;
    var index = res.target.id;
    if (index > -1) {
      fileList.splice(index,1);
      tempFilePaths.splice(index, 1);
    }
    this.setData({
      fileList:fileList
    })
    this.data.tempFilePaths = tempFilePaths;
  },
  //会员制度
  onRules: function (e) {
    var config = wx.getStorageSync('config');
    app.webview(app.apiroot+'/app/'+config.app_id+'/ais/article/sla');
  },
})